﻿using System;
using System.Data;
using System.Data.SqlClient;
namespace DataLayer
{
    public class DbSqlClient : Database
    {
        SqlDataReader rd = null;
        SqlTransaction trn = null;

        public override IDbConnection CreateConnection()
        {
            return new SqlConnection();
        }

        public override IDbConnection CreateConnection(string cnnstr)
        {
            return new SqlConnection(cnnstr);
        }

        public override IDbCommand CreateCommand()
        {
            IDbCommand cmd = new SqlCommand();
            cmd.CommandTimeout = int.MaxValue;
            return cmd;
        }

        public override IDbTransaction CreateTransaction()
        {
            return trn;
        }

        public override IDataReader CreateReader()
        {
            return rd;
        }

        public override IDbCommand CreateCommand(string commandText, IDbConnection connection)
        {
            IDbCommand cmd = new SqlCommand(commandText, (SqlConnection)connection);
            cmd.CommandTimeout = int.MaxValue;
            return cmd;
        }

        public override IDbDataAdapter CreateAdapter(string commandText, string connectionString)
        {
            IDbDataAdapter adp = new SqlDataAdapter(commandText, connectionString);
            adp.SelectCommand.CommandTimeout = int.MaxValue;
            return adp;
        }

        public override IDbDataAdapter CreateAdapter(IDbCommand cmd)
        {
            IDbDataAdapter adp = new SqlDataAdapter((SqlCommand)cmd);
            adp.SelectCommand.CommandTimeout = int.MaxValue;
            return adp;
        }

        public override IDbDataAdapter CreateAdapter()
        {
            IDbDataAdapter adp = new SqlDataAdapter();
            adp.SelectCommand.CommandTimeout = int.MaxValue;
            return adp;
        }

        public override IDbCommand CreateCommand(string commandText, IDbConnection connection, IDbTransaction trn)
        {
            IDbCommand cmd = new SqlCommand(commandText, (SqlConnection)connection, (SqlTransaction)trn);
            cmd.CommandTimeout = int.MaxValue;
            return cmd;
        }

        public override IDbDataAdapter CreateAdapter(string commandText, IDbConnection connection)
        {
            IDbDataAdapter adp = new SqlDataAdapter(commandText, (SqlConnection)connection);
            adp.SelectCommand.CommandTimeout = int.MaxValue;
            return adp;
        }

        public override IDbCommand CreateCommand(CommandType commandType, string commandText)
        {
            SqlCommand cmd = new SqlCommand(commandText);
            cmd.CommandType = commandType;
            cmd.CommandTimeout = int.MaxValue;
            return cmd;
        }

        public override IDbDataParameter ParamAddWithValue(string paramName, object value)
        {
            return new SqlParameter(paramName, value);
        }

        public override void DeriveCommand(IDbCommand cmd)
        {
            SqlCommandBuilder.DeriveParameters((SqlCommand)cmd);
        }

        public override IDbCommand CreateCommand(string commandText)
        {
            IDbCommand cmd = new SqlCommand(commandText);
            cmd.CommandTimeout = int.MaxValue;
            return cmd;
        }

        public override string GetParameterToken()
        {
            return "@";
        }

        public override IDbDataParameter CreateParam<T>(string paramName, DbType tip, T value)
        {
            SqlParameter prm = new SqlParameter();
            prm.ParameterName = paramName;
            prm.Value = value;
            prm.DbType = tip;
            return prm;
        }

        public override IDbDataParameter CreateParam<T>(string paramName, DbType tip, T value, int Size)
        {
            SqlParameter prm = new SqlParameter();
            prm.ParameterName = paramName;
            prm.Value = value;
            prm.DbType = tip;
            prm.Size = Size;
            return prm;
        }

        public override IDbDataParameter CreateParam(string paramName, DbType tip, object value, int Size)
        {
            SqlParameter prm = new SqlParameter();
            prm.ParameterName = paramName;
            prm.Value = value;
            prm.DbType = tip;
            prm.Size = Size;
            return prm;
        }

        public override IDbDataParameter CreateParam(string paramName, DbType tip, object value)
        {
            SqlParameter prm = new SqlParameter();
            prm.ParameterName = paramName;
            prm.Value = value;
            prm.DbType = tip;
            return prm;
        }

        public override IDbDataParameter CreateParam(string paramName, int Size, DbType tip)
        {
            SqlParameter prm = new SqlParameter();
            prm.ParameterName = paramName;
            prm.DbType = tip;
            prm.Size = Size;
            return prm;
        }
        public override IDbDataParameter CreateParam(string paramName, DbType tip)
        {
            SqlParameter prm = new SqlParameter();
            prm.ParameterName = paramName;
            prm.DbType = tip;
            return prm;
        }
    }
}
